package com.lcg.word

import com.lcg.base.Loading
import com.lcg.base.net.download
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch
import java.io.File

/**
 * 工具类
 *
 * @author lei.chuguang Email:475825657@qq.com
 */
fun Loading.launched(
    scope: CoroutineScope,
    msg: String = "加载中...",
    block: suspend CoroutineScope.() -> Unit
) {
    scope.launch {
        loadingMessage = msg
        show(this.coroutineContext[Job])
        block.invoke(this)
        dismiss()
    }
}

/**缓存文件到本地*/
fun cacheFile(url: String, success: ((File?) -> Unit)? = null) {
    MainScope().launch { success?.invoke(download(url)) }
}